Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch: DecodeEntity doesn't honor contentType #142

Merged

Conversation

denopink
Copy link
Contributor

@denopink denopink commented Oct 27, 2023

The issue lie's in this section:

msg, ok := wrpcontext.Get[*wrp.Message](original.Context())
if ok {
jsonBytes, err := json.Marshal(msg)
if err != nil {
return nil, err
}
entity := &Entity{
Message: *msg,
Format: format,
Bytes: jsonBytes,
}
return entity, nil
}

Where it sets entity.Bytes to a set of json bytes instead of honoring the content-type format

This eventually becomes a problem when a user sends a msgpack request to talaria and the device receiving that message then expects a msgpack request body... triggering a decoding error on the device's end

Because candlelight eats the request body when it extracts the wrp message:

wrp-go/wrphttp/decoders.go

Lines 111 to 140 in e3db47f

func DecodeRequest(r *http.Request, msg any) (*http.Request, error) {
if _, ok := wrpcontext.Get[*wrp.Message](r.Context()); ok {
// Context already contains a message, so just return the original request
return r, nil
}
format, err := DetermineFormat(wrp.JSON, r.Header, "Content-Type")
if err != nil {
return nil, fmt.Errorf("failed to determine format of Content-Type header: %v", err)
}
var decodedMessage wrp.Message
// Try to decode the message using the HTTP Request headers
// If this doesn't work, decode the message as Msgpack or JSON format
if err = SetMessageFromHeaders(r.Header, &decodedMessage); err != nil {
// Msgpack or JSON Format
bodyReader := r.Body
err = wrp.NewDecoder(bodyReader, format).Decode(&decodedMessage)
if err != nil {
return nil, fmt.Errorf("failed to decode wrp message: %v", err)
}
}
ctx := wrpcontext.Set(r.Context(), &decodedMessage)
// Return a new request with the new context, containing the decoded message
return r.WithContext(ctx), nil
}

the solution appears to be to save the request body in the context along with the wrp message. This allows us to keep the api changes to a minimum without introducing larger patch

This patch will require a one liner update to candlelight and scytale.

@denopink denopink added the bug Something isn't working label Oct 27, 2023
@denopink denopink self-assigned this Oct 27, 2023
Copy link
Contributor

@maurafortino maurafortino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me :)

@codecov
Copy link

codecov bot commented Oct 27, 2023

Codecov Report

Merging #142 (8ade81d) into main (b1746ad) will decrease coverage by 0.06%.
Report is 1 commits behind head on main.
The diff coverage is 52.17%.

@@            Coverage Diff             @@
##             main     #142      +/-   ##
==========================================
- Coverage   47.79%   47.74%   -0.06%     
==========================================
  Files          24       24              
  Lines        4352     4344       -8     
==========================================
- Hits         2080     2074       -6     
+ Misses       2087     2081       -6     
- Partials      185      189       +4     
Flag Coverage Δ
unittests 47.74% <52.17%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
wrphttp/decoders.go 67.94% <52.17%> (-0.66%) ⬇️

@denopink denopink merged commit 3460c43 into main Oct 30, 2023
13 of 14 checks passed
@denopink denopink deleted the denopink/patch/wrp-DecodeEntity-doesnt-honor-contentType branch October 30, 2023 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants